package start.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import start.model.Message;
import start.service.IMsgService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MessageCtrl {
	@Resource
	private IMsgService service;
	@RequestMapping("/message/query")
	public Map<String,Object> query(int page,int rows,String json){
		
		Map<String,Object> map = JSON.parseObject(json);
		
		PageHelper.startPage(page,rows);
		List<Message> list = this.service.query(map);
		long total = ((Page<Message>)list).getTotal();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("toal", total);
		data.put("rows", list);
		return data;
	}
	
	
	@RequestMapping("/message/save")
	public boolean save(Message bean) {
		try {
			this.service.save(bean);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@RequestMapping("/message/remove/{id}")
	public boolean remove(@PathVariable("id")  int id) {
		try {
			this.service.remove(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@RequestMapping("/message/get/{id}")
	public Message get(@PathVariable("id") int id) {
		return this.service.get(id);
	}
	
}
